//
//  LPPTimeController.m
//  Lottery
//
//  Created by 刘盼 on 16/1/24.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPTimeController.h"

@interface LPPTimeController ()

@property(nonatomic,strong)UIDatePicker *picker;
@end

@implementation LPPTimeController

#pragma mark--------<UITableViewDelegate>---------

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return;
    }
    //创建日期管理器
    UIDatePicker *picker = [[UIDatePicker alloc]init];
    //设置语言
    picker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];
    //格式
    picker.datePickerMode = UIDatePickerModeTime;
    
    self.picker = picker;
    //创建日期管理器上的toolbar
    UIToolbar *tools = [[UIToolbar alloc]init];
    //取消按钮
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    //弹簧按钮（占满中间时第三个按钮靠最右边）
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    //完成按钮
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    
    tools.items = @[item1,item2,item3];
    
    UITextField *text = [[UITextField alloc]init];
    //获取点击的cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell.contentView addSubview:text];
    //把日期管理器设置为键盘
    text.inputView = picker;
    
    CGRect barFrame = tools.frame;
    
    barFrame.size.height = 44;
    
    tools.frame = barFrame;
    
    text.inputAccessoryView = tools;
    //text成为第一响应者
    [text becomeFirstResponder];
    
}
#pragma mark--------toolbar点击事件

-(void)cancel {
    
    [self.view endEditing:YES];
    
}

-(void)finish {
  
    NSDate *date = self.picker.date;
    //创建一个格式化的时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //时间格式
    formatter.dateFormat = @"HH:mm";
    
    NSString *time = [formatter stringFromDate:date];
    //获取选中的位置
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    //获取选中的cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.detailTextLabel.text = time;
    
    [self.view endEditing:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}





@end
